package com.swt.spider;

import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class HttpTest {
    public static void main(String[] args) {


        JSONObject object = new JSONObject();
        object.set("currentPage", 1);
        object.set("regionCode", "420000000000");
        object.set("onlyLocalLevel", "");
        object.set("areaGrade", 2);
        object.set("keyword", "");


        String httpRequest = HttpRequest.post("http://zwfw.hubei.gov.cn/web/search/item/item_transaction_num")
                .header("Connection", "Keep-alive")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("Cookie", "Hm_lvt_5544783ae3e1427d6972d9e77268f25d=1597226115; SESSIONID=MzUzOWRjZjctODUyYy00MzdkLTgyZWItNDNkOTI4YTAwODJk; Hm_lvt_2e78e4e6592603c96de5e3ada929877c=1596679112,1597226479,1597635483; Hm_lpvt_2e78e4e6592603c96de5e3ada929877c=1597636116")
                .header("Origin", "http://zwfw.hubei.gov.cn")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36")
                .body(object.toStringPretty())
                .execute()
                .body();
        System.out.println(httpRequest);

        JSONObject jsonObject = JSONUtil.parseObj(httpRequest);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray result = data.getJSONArray("result");

        CsvWriter writer = new CsvWriter("E:\\hubei\\1.csv");

        for (int i2 = 0; i2 < result.size(); i2++) {
            String[] strings = new String[2];
            JSONObject obj = result.getJSONObject(i2);
            String id = obj.getStr("id");
            String isOnline = obj.getStr("isOnline");
            strings[0] = id;
            strings[1] = isOnline;
            writer.write(strings);
        }

        writer.close();

        System.err.println(result.toStringPretty());


    }


}

